package com.ybkj.daijia.server.service.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.config.GasStationApi;
import com.ybkj.daijia.server.gasstation.Discount;
import com.ybkj.daijia.server.service.GasDiscountService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

@Service
public class DefaultDiscountService implements GasDiscountService {

    private static final Logger logger = LoggerFactory.getLogger(DefaultDiscountService.class);

    @Autowired
    private GasStationApi gasStationApi;

    @Autowired
    private SettingUtils settingUtils;

    @Override
    public List<Discount> findBypassengerGreadeId(Long passengerGreadeId) {
        List<Discount> list = new ArrayList<Discount>();

        StringBuilder builder = new StringBuilder();
        builder.append(gasStationApi.getGasStationBaseURL()).append(GasStationApi.QUERY_BY_GRADEID);

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("passengerGreadeId", String.valueOf(passengerGreadeId));
        builder.append("?passengerGreadeId={passengerGreadeId}");

        String result = gasStationApi.get(builder.toString(), kv, settingUtils.get());
        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray rows = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                list = gson.fromJson(rows.toString(), new TypeToken<List<Discount>>() {
                }.getType());
            }
        }
        return list;
    }

    @Override
    public Discount queryOneGrade(Long passengerGreadeId, Long gasStationId) {

        StringBuilder builder = new StringBuilder();
        builder.append(gasStationApi.getGasStationBaseURL()).append(GasStationApi.QUERY_ONE_GRADE);

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("passengerGreadeId", String.valueOf(passengerGreadeId));
        kv.put("gsId", String.valueOf(gasStationId));
        builder.append("?passengerGreadeId={passengerGreadeId}&gsId={gsId}");

        String result = gasStationApi.get(builder.toString(), kv, settingUtils.get());
        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                Discount discount = gson.fromJson(data.toString(), Discount.class);
                return discount;
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> update(Discount discount) {
        Map<String, Object> map = new HashMap<String, Object>();

        StringBuilder sb = new StringBuilder();
        sb.append(gasStationApi.getGasStationBaseURL()).append(GasStationApi.UPDATE_BY_GRADEID);

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("passengerGradeId", String.valueOf(discount.getPassengerGradeId()));
        kv.add("gsId", String.valueOf(discount.getGsId()));
        kv.add("discount", String.valueOf(discount.getDiscount()));

        String resultStr = gasStationApi.postMultiValueMap(sb.toString(), kv, settingUtils.get());
        JSONObject jo = new JSONObject(resultStr);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("code", 0);
        } else {
            map.put("code", -1);
            map.put("message", jo.getString("message"));
        }
        return map;
    }

    @Override
    public Map<String, Object> create(Discount discount) {
        Map<String, Object> map = new HashMap<String, Object>();

        StringBuilder sb = new StringBuilder();
        sb.append(gasStationApi.getGasStationBaseURL()).append(GasStationApi.CREATE_BY_GRADEID);

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("passengerGradeId", String.valueOf(discount.getPassengerGradeId()));
        kv.add("gsId", String.valueOf(discount.getGsId()));
        kv.add("discount", String.valueOf(discount.getDiscount()));

        String resultStr = gasStationApi.postMultiValueMap(sb.toString(), kv, settingUtils.get());
        JSONObject jo = new JSONObject(resultStr);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("code", 0);
        } else {
            map.put("code", -1);
            map.put("message", jo.getString("message"));
        }
        return map;
    }

}
